package com.ylkj;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ylkj.common.utils.SpellProxyUtil;



public class CodeList {
	public static void main2(String[] args) throws SQLException {
		System.out.println(SpellProxyUtil.convertWordToSpell("通州区"));
//		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-datasource.xml");
//		DataSource dataSource=ctx.getBean("dataSourceForManagementBack",DataSource.class);
//		String sql="select * from up_codelist WHERE `codetype` IN ('city','province')";
//		Connection connection=dataSource.getConnection();
//		Statement stm=connection.createStatement();
//		ResultSet rs=stm.executeQuery(sql);
//		while(rs.next())
//		{
//			int result = stm1.executeUpdate(sql);
//			System.out.println("用户名为:");
//		    System.out.println(SpellProxyUtil.convertWordToInitial(rs.getString("code_name")));
//		    System.out.println(rs.getString("code_name"));
//		    System.out.println(SpellProxyUtil.convertWordToSpell(rs.getString("code_name")));
//		}
	}
	
	public static void main(String[] args) throws SQLException {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-datasource.xml");
		DataSource dataSource=ctx.getBean("dataSourceForManagementBack",DataSource.class);
		String sql="select * from up_codelist WHERE `codetype` IN ('city','province')";
		Connection connection=dataSource.getConnection();
		Statement stm=connection.createStatement();
		Statement stm1=connection.createStatement();
		ResultSet rs=stm.executeQuery(sql);
		String content="";
		while(rs.next())
		{   
			content = content +" \n "+ "update up_codelist set name_spell_simple='"
					+ SpellProxyUtil.convertWordToInitial(rs.getString("code_name")) + "',name_spell='"
					+ SpellProxyUtil.convertWordToSpell(rs.getString("code_name")) + "' where id='"
							+ rs.getString("id")+"';";
			//System.out.println(content);
		
//			int result = stm1.executeUpdate(sql);
//			System.out.println("用户名为:");
//		    System.out.println(SpellProxyUtil.convertWordToInitial(rs.getString("code_name")));
//		    System.out.println(rs.getString("code_name"));
//		    System.out.println(SpellProxyUtil.convertWordToSpell(rs.getString("code_name")));
		}
		
		FileOutputStream fop = null;
		  File file;

		  try {

			   file = new File("d://newfile1.txt");
			   fop = new FileOutputStream(file);

			   // if file doesnt exists, then create it
			   if (!file.exists()) {
			    file.createNewFile();
			   }

			   // get the content in bytes
			   byte[] contentInBytes = content.getBytes();

			   fop.write(contentInBytes);
			   fop.flush();
			   fop.close();

			   System.out.println("Done");

		  } catch (Exception e) {
		   e.printStackTrace();
		  } finally {
		   try {
		    if (fop != null) {
		     fop.close();
		    }
		   } catch (IOException e) {
		    e.printStackTrace();
		   }
		  }
	}

}
